@extends('admin.public.header')
@section('title','批量发货')
@section('listcontent')
    <form class="layui-form">
        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label required">导入模板</label>
                <div class="layui-input-block" style="width:500px;">
                    <input name="url" lay-verify="required" placeholder="请选择模板" value="" class="layui-input layui-col-xs6" lay-reqtext="请上传模板" disabled >
                    <button style="display: inline-block;position: absolute;right: 0px;" type="button" class="layui-btn" id="template"><i class="layui-icon"></i>选择模板</button>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">快递物流：</label>
                <div class="layui-input-block" style="width:500px;">
                    <select name="express" lay-verify="required" lay-reqtext="请选择快递物流" lay-search>
                        <option value="">请选择快递物流</option>
                        @foreach($express_list as $key => $list)
                        <option value="{{ $list['name'] }}">{{ $list['name'] }}</option>
                        @endforeach
                    </select>   
                </div>  
            </div>
            
            <div class="hr-line"></div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" id="saveBtn" lay-submit lay-filter="saveBtn">发 货</button>
                    <a class="layui-btn layui-btn-normal" href="{{ $realPath }}" download="{{ $fileName }}.csv">默认格式下载</a>
                </div>
            </div>
        </div>
    </form>
    <fieldset class="layui-elem-field layui-field-title layui-hide send" style="margin-top: 30px;">
        <legend>发货列表</legend>
    </fieldset>
    <table class="layui-table layui-hide send">
        <thead>
            <tr>
                <th>订单编号</th>
                <th>下单用户</th>
                <th>收货人信息</th>
                <th>物流公司</th>
                <th>物流单号</th>
                <th>订单状态</th>
            </tr>
        </thead>
        <tbody class="sendList">
            
        </tbody>
    </table>
@endsection

@section('listscript')
    <script type="text/javascript">
        layui.use(['iconPickerFa', 'form', 'layer', 'upload', 'xmSelect','miniTab'], function () {
            var iconPickerFa = layui.iconPickerFa,
                form = layui.form,
                layer = layui.layer,
                upload = layui.upload,
                xmSelect = layui.xmSelect,
                miniTab = layui.miniTab,
                $ = layui.$;
                miniTab.listen();
            
            //上传文件
            upload.render({
                elem: '#template'
                ,url: '/admin/upload/uploadFile' //改成您自己的上传接口
                ,accept: 'file'
                ,acceptMime: '.csv,.xls,.xlsx'
                ,exts: 'xls|xlsx|csv'
                ,size: 1024*4 //限制文件大小，单位 KB
                ,headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }
                ,done: function(res){
                    if(res.code==0){
                        layer.msg("上传成功",{icon: 1});
                        var domain = window.location.host;
                        
                        $("input[name='url']").val(res.data[0]);
                    }else{
                        layer.msg(res.message,{icon: 2});
                        $("input[name='url']").val('');
                    }
                }
            });
            //监听提交
            form.on('submit(saveBtn)', function(data){
                //$("#saveBtn").addClass("layui-btn-disabled");
                //$("#saveBtn").attr('disabled', 'disabled');
                //var loading = layer.msg('加载中..', {icon: 16,shade: 0.3,time: false});
                $.ajax({
                    url:'/admin/order/batchSend',
                    type:'post',
                    data:data.field,
                    dataType:'JSON',
                    headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                    success:function(res){
                        //layer.close(loading);  //返回数据关闭loading
                        if(res.code==0){
                            $(".send").removeClass("layui-hide");
                            layer.msg(res.message,{icon: 1});
                            if(res.data.list.length > 0){
                                var html = '';
                                $.each(res.data.list,function(i,item){
                                    html += '<tr><td>'+item.order_no+'</td><td>'+item.nickname+'</td><td><span>'+item.name+'</span><span style="margin-left:15px;">'+item.mobile+'</span><br/><span>'+item.address+'</span></td><td>'+item.express+'</td><td>'+item.express_no+'</td><td><span style="color:#1E9FFF;">'+item.order_status_name+'</span></td></tr>';
                                });
                                $(".sendList").empty();
                                $(".sendList").append(html);
                            }
                        }else{
                            layer.msg(res.message,{icon: 2});
                            $("#saveBtn").removeClass("layui-btn-disabled");
                            $("#saveBtn").removeAttr('disabled');
                        }
                    },
                    error:function (data) {
                        layer.msg(res.message,{icon: 2});
                        $("#saveBtn").removeClass("layui-btn-disabled");
                        $("#saveBtn").removeAttr('disabled');
                    }
                });
                return false;
            });

        });
        
    </script>
@endsection